﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Friend NotInheritable Class RuleSetDefinitionEditor
        Inherits UITypeEditor
        ' Methods
        Public Overrides Function EditValue(ByVal typeDescriptorContext As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal o As Object) As Object
            If (typeDescriptorContext Is Nothing) Then
                Throw New ArgumentNullException("typeDescriptorContext")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Dim obj2 As Object = o
            Dim loader As WorkflowDesignerLoader = [TryCast](serviceProvider.GetService(GetType(WorkflowDesignerLoader)),WorkflowDesignerLoader)
            If ((Not loader Is Nothing) AndAlso loader.InDebugMode) Then
                Throw New InvalidOperationException(Messages.DebugModeEditsDisallowed)
            End If
            Dim instance As RuleSetReference = [TryCast](typeDescriptorContext.Instance,RuleSetReference)
            If (((instance Is Nothing) OrElse (instance.RuleSetName Is Nothing)) OrElse (instance.RuleSetName.Length <= 0)) Then
                Throw New ArgumentException(Messages.RuleSetNameNotSet)
            End If
            Dim component As Activity = Nothing
            Dim service As IReferenceService = [TryCast](serviceProvider.GetService(GetType(IReferenceService)),IReferenceService)
            If (Not service Is Nothing) Then
                component = [TryCast](service.GetComponent(typeDescriptorContext.Instance),Activity)
            End If
            Dim ruleSets As RuleSetCollection = Nothing
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(serviceProvider, Helpers.GetRootActivity(component))
            If (Not definitions Is Nothing) Then
                ruleSets = definitions.RuleSets
            End If
            If ((Not ruleSets Is Nothing) AndAlso Not ruleSets.Contains(instance.RuleSetName)) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.RuleSetNotFound, New Object() { instance.RuleSetName }))
            End If
            Me.editorService = DirectCast(serviceProvider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If (Not Me.editorService Is Nothing) Then
                Dim ruleSet As RuleSet = ruleSets.Item(instance.RuleSetName)
                [Using] dialog As RuleSetDialog = New RuleSetDialog(component, ruleSet)
                    If (DialogResult.OK = Me.editorService.ShowDialog(dialog)) Then
                        obj2 = dialog.RuleSet
                    End If
                End [Using]
            End If
            Return obj2
        End Function

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function


        ' Fields
        Private editorService As IWindowsFormsEditorService
    End Class
End Namespace

